{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Theoretical Distributions](05.02-Theoretical-Distributions.ipynb) | [Contents](Index.ipynb) | [KS Test](05.04-KS-Test.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3 t检验\n",
    "\n",
    "在统计学中，我们假设两个不同的随机变量拥有相同的均值或有相同的方差，或他们遵循相同的分布。为了检验这些假设，推到了检验统计量，并基于检验统计学，假设被拒绝或接受。当检验统计服从t分布时，t检验被用于检验假设。这个假设在`scipy.stats`[库](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_1samp.html)可用。让我们首先检验是否随机变量的均值同我们预期的相同。`st.ttest_1samp`函数则能达到这个目的。我们生成均值为5，标准差为10的正态分布随机变量。我们将检验是否生成的随机变量的均值为5。因为我们在说的是容量为1000的样本，我们期待均值在大多数时候(但并非总是)会接近于5。hypothetis基于p值被拒绝或接受。p值近似于1意味着假设是正确的；p值接近于零意味着假设被拒绝。显著性水平($\\alpha$)用于定义阈值，它经常取0.05或0.01。如果p值比这个值更大，则我们可以接受这个假设。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0879512569494\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as st\n",
    "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n",
    "#t-检验\n",
    "t,p = st.ttest_1samp(rvs1,5)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个例子中我们可以看到p值为0.88，这意味着生成的随机变量的均值近似于5。t检验也被用于检验两个独立随机数的均值是否相等。让我们生成两个具有相同的均值为5的正态分布随机变量。我们想要看看这两组独立变量的均值是否相同。我们可以使用`st.ttest_ind`达到这个目的。在这个例子中p值为0.958，这意味着均值相等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.958036907764\n"
     ]
    }
   ],
   "source": [
    "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n",
    "rvs2 = st.norm.rvs(loc=5,scale=10,size=1000)\n",
    "# t-检验\n",
    "t, p = st.ttest_ind(rvs1,rvs2)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在前面的例子中，我们检验了两个独立样本的均值。当样本相关或来自相同的实验，我们也可以检验均值是否相同。我们可以使用`st.ttest_ind`达到这个目的。我们获得的p值为0.64，这意味着均值相等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.643099012567\n"
     ]
    }
   ],
   "source": [
    "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n",
    "rvs2 = st.norm.rvs(loc=5,scale=10,size=1000)\n",
    "# t-检验\n",
    "t, p = st.ttest_rel(rvs1,rvs2)\n",
    "print(p)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
